package views.components
{
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	import models.MainModel;
	import models.types.SectionType;
	import models.types.SubsectionType;
	
	public class Subsection extends Sprite
	{
		private var _data:SubsectionType;
		
		private var _id:int;
		
		private var _mc:SubsectionMC;
		private var _line:Sprite;

		private var _hitarea:MovieClip;
		
		public function Subsection(data:SubsectionType):void
		{
			_data = data;
			
			_id = _data.id;
			
			_mc = new SubsectionMC();
			
			addChild(_mc);
//
			fillText();
			
			_line = drawArea(0xFFFFFF, 0, 0, _mc.caption_txt.width - 5, 1);
			
			_hitarea = _mc.Hitarea;
			
			_line.x = 2;
			_line.y = _mc.caption_txt.height - 3;
			
			addChild(_line);
			
			_hitarea.buttonMode = true;
			_hitarea.addEventListener(MouseEvent.ROLL_OVER, mouseHandler);
			_hitarea.addEventListener(MouseEvent.ROLL_OUT, mouseHandler);
			_hitarea.addEventListener(MouseEvent.CLICK, mouseHandler);
		}
		
		public function activate():void
		{
			_hitarea.mouseEnabled = true;
		}
		
		public function deactivate():void
		{
			_hitarea.mouseEnabled = false;
		}
		
		public function hide():void
		{
			Tweener.removeTweens(this);
			this.alpha = 0;
		}
		
		public function show(val:Number = 0):void
		{
			Tweener.addTween(this, {delay:val, alpha: 1, time:0.1, transition:Equations.easeOutQuad } );
		}
		
		private function mouseHandler(e:MouseEvent):void 
		{
			switch(e.type)
			{
				case MouseEvent.ROLL_OVER:
					mouseOver();
					break;
				case MouseEvent.ROLL_OUT:
					mouseOut();
					break;
				case MouseEvent.CLICK:
					navigateToURL(new URLRequest(_data.url), "_self");
					break;
			}
		}
		
		private function mouseOver():void
		{
			Tweener.addTween(_mc.Background, { alpha: 1, time:0.1, transition:Equations.easeOutQuad } );
		}
		
		private function mouseOut():void
		{
			Tweener.addTween(_mc.Background, { alpha: 0, time:0.1, transition:Equations.easeOutQuad } );
		}
		
		private function fillText():void
		{
			_mc.caption_txt.autoSize = TextFieldAutoSize.LEFT;
			_mc.caption_txt.text = _data.caption;
		}

		private function drawArea(c:uint, x:Number, y:Number, w:Number, h:Number, a:Number = 1):Sprite
		{
			var area:Sprite = new Sprite();
			
			area.graphics.beginFill(c, a);
			area.graphics.drawRect(x, y, w, h); 
			area.graphics.endFill();
			
			return area
		}
	}

}